package view;

import controllers.SudokuController;
import java.awt.*;
import java.awt.event.WindowAdapter;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import models.Jugador;
import models.TopTen;
import org.netbeans.lib.awtextra.AbsoluteConstraints;
import org.netbeans.lib.awtextra.AbsoluteLayout;

/**
 * Vista Top Ten
 */
public class TopTenView {
    private JButton jButton1;
    private JButton jButton2;
    private TopTen top;
    private JFrame jfWindow;
    
    /**
     * Creates new form TopTenView
     */
    public TopTenView(TopTen newTop) {
        this.top = newTop;        
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e){
            e.printStackTrace();
        }
        jfWindow = new JFrame();
        jfWindow.setMinimumSize(new Dimension(100,470));        
        //Segun la dimension de la pantalla (resolucion) tamaño y posicion de la ventana default
        Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
        Point framePos=new Point((int)(screenSize.width)/2-200,(int)(screenSize.height)/2-235);
        jfWindow.setLocation(framePos);
        //Renombro el cartel
        jfWindow.setTitle("Top Ten");
        jfWindow.setIconImage(Toolkit.getDefaultToolkit().getImage(IMainView.class.getResource("/resources/sudoku-icon.png")));
        //Configuro los Contenedores de la Ventana
        Container container = jfWindow.getContentPane();
        setupMainContainer(container);
        //Configuro las caracteristicas de la Ventana
        jfWindow.pack();
        jfWindow.setVisible(true);
        jfWindow.setResizable(false);
        jfWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);            
    }
    
    /**
     * Inicializa el Contenedor Principal
     * @param container 
     */
    private void setupMainContainer(Container container) {
        container.setLayout(new BorderLayout());
        container.add(this.selector(),BorderLayout.PAGE_START);
        
        JPanel Botones = new JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();       
        jfWindow.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        

        jButton1.setText("Aceptar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Limpiar Lista");
        jButton2.addActionListener(SudokuController.getInstance());
        jButton2.setActionCommand("CLEAR_LIST");
        Botones.add(jButton1);//Botones.add(jButton2);
        container.add(Botones,BorderLayout.PAGE_END);
        container.add(dificultyPanel("EASY"),BorderLayout.CENTER);
    }
    
    /**
     * Accion boton Cerrar top-ten
     * @param evt 
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        jfWindow.dispose();
    }
    
    /**
     * Accion cambiar de dificultad
     * @param evt 
     */
    private void cambiarDiff(java.awt.event.ActionEvent evt) {
        Container container = jfWindow.getContentPane();
        container.remove(2);
        container.add(dificultyPanel(evt.getActionCommand()),BorderLayout.CENTER);
        
        jfWindow.invalidate();
        jfWindow.validate();
        jfWindow.repaint();
    }
    
    /**
     * Panel selector de dificultad
     * @return 
     */
    private JPanel selector(){
        JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Dificultad", TitledBorder.CENTER, TitledBorder.TOP, null, null));
        
        JRadioButton facil = new JRadioButton(); 
        facil.setActionCommand("EASY");
        facil.setText("Facil");
        JRadioButton medio = new JRadioButton();
        medio.setActionCommand("MODERATE");
        medio.setText("Medio");
        JRadioButton dificil = new JRadioButton();
        dificil.setActionCommand("HARD");
        dificil.setText("Dificil");
        ButtonGroup group = new ButtonGroup();
        group.add(facil);
        group.add(medio);
        group.add(dificil);
        facil.setSelected(true);
        
        facil.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cambiarDiff(evt);
            }
        });
        medio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cambiarDiff(evt);
            }
        });
        dificil.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cambiarDiff(evt);
            }
        });
        panel.add(facil);panel.add(medio);panel.add(dificil);
        
        return panel;
    }
    
    /**
     * Panel Dificultad, muestra el top ten segun dificultad
     * @param diff "EASY" "MODERATE" "HARD"
     * @return 
     */
    private JPanel dificultyPanel(String diff){
        JPanel superior = new JPanel();
        superior.setLayout(new AbsoluteLayout());
        superior.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), diff, TitledBorder.CENTER, TitledBorder.TOP, null, null));
        
        JPanel panel = new JPanel();
        Dimension dim = new Dimension(270,30*top.getList(diff).size()+30);
        panel.setMaximumSize(dim);
        panel.setPreferredSize(dim);
        panel.setSize(dim);        
        panel.setLayout(new GridLayout(top.getList(diff).size()+1,3));
        
        JLabel nameEtq = new JLabel("Nombre");
        nameEtq.setForeground(Color.red);
        nameEtq.setFont(new java.awt.Font("Tahoma", 2, 20));
        
        JLabel tiempoEtq = new JLabel("Tiempo");
        tiempoEtq.setForeground(Color.red);
        tiempoEtq.setFont(new java.awt.Font("Tahoma", 2, 20));
        
        JLabel puntosEtq = new JLabel("Puntaje");
        puntosEtq.setForeground(Color.red);
        puntosEtq.setFont(new java.awt.Font("Tahoma", 2, 20));
        
        panel.add(nameEtq);
        panel.add(puntosEtq);
        panel.add(tiempoEtq);
                
        for (Jugador player : top.getList(diff)) {
            JLabel name = new JLabel();
            name.setFont(new java.awt.Font("Tahoma", 0, 16));
            name.setHorizontalTextPosition(JLabel.CENTER);
            JLabel tiempo = new JLabel();
            tiempo.setFont(new java.awt.Font("Tahoma", 0, 16));
            tiempo.setHorizontalTextPosition(JLabel.CENTER);
            JLabel puntos = new JLabel();
            puntos.setFont(new java.awt.Font("Tahoma", 0, 16));
            puntos.setHorizontalTextPosition(JLabel.CENTER);
            name.setText(player.getNombre());
            puntos.setText(String.valueOf(player.getPuntuacion()));
            tiempo.setText(player.getTiempo().toString());
            panel.add(name);
            panel.add(puntos);
            panel.add(tiempo);
        }
        superior.add(panel,new AbsoluteConstraints(10, 15, -1, -1));        
        return superior;       
    }
        
    /**
    * Permite Agregar un listener a la ventana
    * @param windowAdapter
    */
    public void addWindowListener(WindowAdapter windowAdapter) {
            jfWindow.addWindowListener(windowAdapter);

    }
}
